package com.example.lotterysystem.service.activitystatus.operater;

import com.example.lotterysystem.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.pojo.dataobject.ActivityPrizeDO;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum targetStatus = convertActivityStatusDTO.getTargetPrizeStatus();

        // 检验传来的 活动Id、奖品Id、奖品状态 是否是存在为空的
        if (activityId == null
                || prizeId == null
                || targetStatus == null) {
            return false;
        }

        // 根据 活动Id 和 奖品Id 组成唯一索引，进行查找对应的 活动奖品关联表中的信息
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectByAPId(activityId, prizeId);
        if (activityPrizeDO == null) {
            return false;
        }

        // 判断当前奖品状态和目标状态是否一致
        if (activityPrizeDO.getStatus()
                .equalsIgnoreCase(targetStatus.name())) {
            return false;
        }

        return true;
    }

    @Override
    public boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            Long activityId = convertActivityStatusDTO.getActivityId();
            Long prizeId = convertActivityStatusDTO.getPrizeId();
            ActivityPrizeStatusEnum targetStatus = convertActivityStatusDTO.getTargetPrizeStatus();
            activityPrizeMapper.updateStatus(activityId,prizeId,targetStatus.name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }
}
